<template>
  <div>
    <ToolBar :multi-selection="multiSelection">
      <div slot="select">
        <el-button size="small" type="danger" @click="batchDelete">批量删除</el-button>
      </div>
      <div slot="unselect" class="unselect">
        <div class="dept">
          <el-select v-model="deptSelection" @change="changeDept">
            <el-option label="所有分组" :value="null">所有分组</el-option>
            <el-option v-for="item in deptList" v-bind:key="item.deptId" :value="item.deptId" :label="item.name" v-text="item.name"></el-option>
          </el-select>
        </div>
        <div class="btn-group">
          <div class="btn">
            <el-button type="text" @click="$router.push({name:'people-join-list'})">申请列表</el-button>
<!--            <div class="count"></div>-->
          </div>
        </div>
      </div>
    </ToolBar>
    <el-table
        :data="peopleList"
        style="width: 100%"
        :row-style="{cursor:'pointer'}"
        @row-click="rowClick"
        @selection-change="handleSelectionChange"
        >
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          width="50"
          align="center"
          label="照片">
        <template slot-scope="scope">
          <el-avatar :size="30" src="https://contacts.zoho.com.cn/file?ID=40772320&fs=thumb">
            <img :src="scope.row.avatar"/>
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column
          prop="nickname"
          align="center"
          label="姓名">
      </el-table-column>
      <el-table-column
          prop="deptName"
          align="center"
          label="部门">
      </el-table-column>
      <el-table-column
          prop="mobile"
          align="center"
          label="手机">
      </el-table-column>
      <el-table-column
          prop="email"
          align="center"
          label="邮箱">
      </el-table-column>
    </el-table>
    <el-dialog :modal-append-to-body="false" title="编辑" @close="hideEdit" :visible="showEdit">
      <Edit ref="editBox" :dept-list="deptList" @submit="savePeople"></Edit>
    </el-dialog>
  </div>
</template>

<script>

import {findPeopleList, findPeopleByName, updatePeople, deletePeopleBatch} from "@/api/auth/tenant";

import ToolBar from '@/components/common/tool-bar'

import {getDeptList} from "@/api/auth/dept";

import Edit from './components/edit-people'

import {deepClone} from "@/libs/util";

export default {
  name: "dept",
  components:{
    ToolBar,
    Edit
  },
  data(){
    return {
      peopleList:[],
      multiSelection: [],
      deptSelection: null,
      deptList:[],
      editPeople: null,
      showEdit: false
    }
  },
  created() {
    this.loadData()
    getDeptList().then(res => {
      if(res.code == 200) {
        this.deptList = res.data
      }
    })
  },methods:{
    loadData(deptId,username) {
      findPeopleList(deptId,username).then(res => {
        if(res.code == 200) {
          this.peopleList = res.data
        }
      })
    },
    changeDept(deptId) {
      this.loadData(deptId)
    },
    handleSelectionChange(val) {
      this.multiSelection = val
    },
    savePeople(form) {
      if(form.userId) {
        updatePeople(form).then(res => {
          if(res.code == 200) {
            this.editPeople = form
            this.$message.success('修改成功')
            this.showEdit = false
          }
        })
      }else {

      }
    },
    hideEdit() {
      this.showEdit = false
      this.$refs.editBox.reset()
    },
    rowClick(row,column,e) {
      let that = this
      console.log(row.index)
      that.showEdit = true
      this.$nextTick(function () {
        that.$refs.editBox.loadData(row)
        that.editPeople = row
      })
    },
    batchDelete() {
      this.$confirm('是否删除'+this.multiSelection.length+'个成员?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let peopleIds = []
        this.multiSelection.forEach(function (item) {
          peopleIds.push(item.userId)
        })
        deletePeopleBatch(peopleIds).then(res => {
          if(res.code == 200) {
            this.$message.success('删除成功')
            this.loadData()
          }
        })
      }).catch(()=>{

      });
    }
  }
}
</script>

<style scoped lang="less">
 .unselect {
   display: flex;
   align-items: center;
   .dept {
     flex: 1;
     display: flex;
     line-height: 20px;
     /deep/ .el-select{
       .el-input {
         width: 120px;
         box-sizing: border-box;
         .el-input__inner {
           width: 120px;
           overflow: hidden;
           text-overflow: ellipsis;
           border:none;
           background: transparent;
           &::placeholder {
             color: #000000;
           }
         }
         .el-icon-arrow-up {
           color: #000000;
         }
       }
     }
   }
   .btn-group {
     display: flex;
     align-items: center;
     .btn {
       margin: 0 5px;
       position: relative;
       .count {
         position: absolute;
         right: -10px;
         top: 0;
         width: 15px;
         height: 15px;
         line-height: 15px;
         text-align: center;
         color: #FFFFFF;
         background: red;
         padding: 2px;
         font-size: 12px;
         transform: scale(0.7);
         border-radius: 50%;
       }
     }
   }
 }
</style>
